#coding=utf-8
import urllib,urllib2
from BeautifulSoup import BeautifulSoup
import MySQLdb
import os
import re
import sys


host   = "96.47.233.104"
user   = "root"
passwd  = "winner"


def en2cn(text):
    values={'hl':'zh-CN','ie':'utf8','text':text,'langpair':"en|zh-CN"}
    url='http://translate.google.cn/translate_t'
    data = urllib.urlencode(values)
    req = urllib2.Request(url, data)
    req.add_header('User-Agent', "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)")
    response = urllib2.urlopen(req) 
    
    html_soup = BeautifulSoup(''.join(response.read()))
    
    html_need = html_soup.findAll("span",{"id":"result_box"})
    #print html_need[0]
    #html_content = html_need[0].findAll("div",{"id":"src-translit"})
    #print html_content
    html_final = html_need[0].findAll('span')
    
    return html_final[0].string
#end def

def cn2en(text):
    values={'hl':'zh-CN','ie':'utf-8','text':text,'langpair':"zh-CN|en"}
    url='http://translate.google.cn/translate_t'
    data = urllib.urlencode(values)
    req = urllib2.Request(url, data)
    req.add_header('User-Agent', "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)")
    response = urllib2.urlopen(req) 
    
    html_soup = BeautifulSoup(''.join(response.read()))
    
    html_need = html_soup.findAll("span",{"id":"result_box"})
    #print html_need[0]
    #html_content = html_need[0].findAll("div",{"id":"src-translit"})
    #print html_content
    html_final = html_need[0].findAll('span')
    
    return unicode(html_final[0].string.decode("utf8"))
#end def

#print (cn2en("如何获得系统的默认编码 ？"))
#print (unicode(en2cn("Beautiful soup")))

if __name__ == '__main__' :
    
    import time
    
    nowtime =  int(time.time())
    #sys.exit(1)
    try:
        conn = MySQLdb.connect(host, user, passwd, db = 'onsme', charset = 'utf8')

        cur = conn.cursor(MySQLdb.cursors.DictCursor)
        
        sql = "select * from dede_archives where id > 316"
        cur.execute(sql)
        ree = cur.fetchall()
        
        a = len(ree)
        
        for i in ree:
            
            try:
                print a
                a = a - 1
                newtitle = en2cn((cn2en(i["title"].encode("utf8"))))
                newkeywd = ((cn2en(i["keywords"].encode("utf8"))))
                newdesc  = en2cn((cn2en(i["description"].encode("utf8"))))
                
                sql = "update dede_archives set title = '%s', keywords = '%s', description = '%s', senddate = %d where id = %d" %\
                    (newtitle, newkeywd, newdesc, nowtime, int(i["id"]))
                
                cur.execute(sql)
                conn.commit()
            except Exception,e:
                print i["id"],
                print "#",
                print e
        #end for
    except Exception,e:
        
        print e
    #end try
    